package com.rzico.order.print;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.alibaba.fastjson.JSONObject;
import com.rzico.util.MD5Utils;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


/**
 * 
 * @author 
 *
 */
public class PrinterUtil {

	public static String url="http://api.zhongwuyun.com";
	public static String appid="8000140";
	public static String appsecret="00cfd4cece2db539b089a05a574c5fad";

	/**
	 * 获取签名
	 *
	 * @return
	 */
	public static String getSign(String printerU,String printerP,String data,String timeStamp) {
		String sign = "appid" +appid
		            + "deviceid" + printerU
		            + "devicesecret"+ printerP
		            + "printdata" + data
		            + "timestamp" + timeStamp
			     	+ appsecret;
		sign = MD5Utils.getMD5Str(sign);
		return sign;
	}

	/**
	 * 发送数据转码
	 * @param s
	 * @return
	 */
	public static String percentEncode(String s) {
		if (s == null) {
			return "";
		}
		try {
			return URLEncoder.encode(s, "UTF-8")
					// OAuth encodes some characters differently:
					.replace("*", "%2A")
					.replace("%7E", "~");
			// This could be done faster with more hand-crafted code.
		} catch (UnsupportedEncodingException wow) {
			throw new RuntimeException(wow.getMessage(), wow);
		}
	}
	/**
	 * 	发送post
	 * @param url 请求url
	 * @param datas 请求数据
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static boolean sendPost(String url,String datas) throws ClientProtocolException, IOException {
		boolean flag = false;
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost post = new HttpPost(url);//构建post请求
		post.setHeader("Content-type","application/x-www-form-urlencoded");//设置请求头
		// 构建消息实体
		StringEntity entity = new StringEntity(datas, Charset.forName("UTF-8"));
		//entity.setContentType("application/x-www-form-urlencoded");//设置请求头
		entity.setContentEncoding("UTF-8");
		post.setEntity(entity);
		CloseableHttpResponse response = httpClient.execute(post);//发送
		if(response.getStatusLine().getStatusCode() == 200) {//请求成功
			if(entity !=null) {
				String str = EntityUtils.toString(response.getEntity());
				JSONObject json = JSONObject.parseObject(str);//解析返回的数据
				JSONObject data = json.getJSONObject("retData");//获取返回的retMsg
				if(data.get("status").equals("1")){//打印机正常
					flag = true;
				}
			}
		}else {
			System.out.println("请求失败");
		}
		return flag;
	}

	public static void print(String printerU,String printerP,String data) {
		String timeStamp = String.valueOf(new Date().getTime());
		String sign = PrinterUtil.getSign(printerU,printerP,data,timeStamp);
		//请求数据
		String datas = "appid="+appid+"&sign="+sign+"&timestamp="+timeStamp+"&deviceid="+
				printerU+"&devicesecret="+printerP+"&printdata="+ PrinterUtil.percentEncode(data);
		try {
			sendPost(url, datas);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
